package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.my.vote.R;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.req.ReqAddVote;
import com.my.vote.pst.info.IPstAddVote;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.IView;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;
import com.sfh.lib.ui.DialogView;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 功能描述:发布投票[报名方式]
 *
 * @author
 * @date 2018/4/11
 */
public class PublishVoteActivity extends AbstractActivity implements IPstAddVote.IVVote {


    public static void startUI(Context context) {
        Intent intent = new Intent(context, PublishVoteActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.etTitle)
    EditText etTitle;
    @BindView(R.id.ivURL)
    ImageView ivURL;
    @BindView(R.id.etDes)
    EditText etDes;
    @BindView(R.id.tvSigUpTime)
    TextView tvSigUpTime;
    @BindView(R.id.tvVoteType)
    TextView tvVoteType;
    @BindView(R.id.tvVoteEndTime)
    TextView tvVoteEndTime;

    ReqAddVote req = new ReqAddVote();

    IPstAddVote pstAddVote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_vote);
        ButterKnife.bind(this);
        tvLeft.setText("Registration Mode");
        tvRight.setText("Add Vote");
        req.setVoteType(1);
        req.setType("1");
    }

    @OnClick({R.id.tvLeft, R.id.tvRight, R.id.ivURL, R.id.tvSigUpTime, R.id.tvVoteType, R.id.tvVoteEndTime, R.id.btOK})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLeft:
                finish();
                break;
            case R.id.tvRight:
                // 调整到-正常模式
                PublishNormalActivity.startUI(this);
                finish();
                break;
            case R.id.ivURL:
                //选择图片
                MultiImageSelector.create().single().showCamera(true).start(this, 100);
                break;
            case R.id.tvSigUpTime:
                // 选择报名结束时间
                selectSigUpTime();
                break;
            case R.id.tvVoteType:
                // 投票类型
                selectSize();
                break;
            case R.id.tvVoteEndTime:
                // 选择投票结束时间
                selectVoteEndTime();
                break;
            case R.id.btOK:
                // 发布
                if (pstAddVote == null) {
                    return;
                }
                req.setTitle(etTitle.getText().toString());
                req.setIntroduce(etDes.getText().toString());
                pstAddVote.addVote(req);
                break;
        }
    }


    private void selectSize() {
        final List<Integer> options1Items = new ArrayList<>(5);
        options1Items.add(1);
        options1Items.add(2);
        options1Items.add(3);
        options1Items.add(4);
        options1Items.add(5);
        options1Items.add(6);
        options1Items.add(7);
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1) + " Votes";
                tvVoteType.setText(tx);
                req.setVoteType(options1Items.get(options1));
            }
        }).build();
        pvOptions.setPicker(options1Items);
        pvOptions.show();
    }

    private void selectVoteEndTime() {
        //投票结束时间选择
        new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
                tvVoteEndTime.setText(format.format(date));
                req.setVoteEndTime((double) date.getTime());
            }
        }).setDate(Calendar.getInstance())
                .setType(new boolean[]{true, true, true, true, true, false}).build().show();

    }

    private void selectSigUpTime() {
        //报名结束时间选择
        new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
                tvSigUpTime.setText(format.format(date));
                req.setEnrollEndTime((double) date.getTime());
            }
        }).setDate(Calendar.getInstance())
                .setType(new boolean[]{true, true, true, true, true, false}).build().show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            if (pstAddVote == null || path == null || path.isEmpty()) {
                return;
            }
            pstAddVote.updateFile(path.get(0));
            Picasso.with(this).load(new File(path.get(0))).into(ivURL);
        }
    }

    @Override
    public void onSuccess() {
        DialogView dialogView = new DialogView.Builder().setTitle("Prompt").setRightText("OK").setShowLeft(false).setMessage("Add Vote Success!").setRightListener(new DialogView.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogFragment dialogFragment, int i) {
                dialogFragment.dismiss();
                finish();
            }
        }).build();
        showDialog(dialogView);
    }

    @Override
    public void onFial(String msg) {
        showToast(msg);
    }

    @Override
    public void onFileSuccess(String url) {
        req.setVoteUrl(url);
    }

    @Override
    public void onFileFial(String msg) {
        showToast(msg);
    }

    @Override
    public IPresenter getPresenter() {
        if (pstAddVote == null) {
            pstAddVote = LoaderService.getService(IPstAddVote.class);
        }
        return pstAddVote;
    }
}
